package puppy.eval;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;

import puppy.graph.AgeAggregator;


import algorithms.PageRankSuggestions;
import algorithms.RandomWalkLatentEngine;

import algorithms.models.QueryTagsModel;
import algorithms.models.QueryTopicsModel;
public class GenerateFeaturesFile {

	// model parameters
	
	//static String path = "../data/graph/last/delcious_graph_merged_enriched_inline_kids.txt";
	static String path = "../data/graph/plus/graph_plus_youth.txt";
	
	//static String path = "../data/graph/plus/graph_adults_sample.txt";
	
	//static String path = "../data/graph/plus/graph_adults_sample.txt";
	//static String path = "../data/graph/last/samplet.txt";
	static String node_node = "../data/graph/last/tags_dmoz_bigram_subset_merged_groups_kids.txt";
	
	static String query_tags = "../data/query_tags/query_and_tags_sorted_aol.txt";
	//static String query_tags="/home/sergio/data/graph/query_tags_plus1.txt";
	
	static String  topics_docs ="../data/topics/kids_simple10/100_output_doc_topics.txt";
	static String  topics_words  ="../data/topics/kids_simple10/topic_given_word_normalized.txt";
	static String  query_topics_path  ="../data/topics/kids_simple10/query/100_output_doc_topics_enriched.txt";
	static String topics_probabilities ="../data/topics/kids_simple10/topics_probabilities.txt";
	
	
	static float epsilon=0.001f;
	static int query_tag_limit=1;
	
	static Double alpha=0.9d;
	static int max_iterations=5;
	static int level=2;
	
	
	int n_gram_weight=2;
	int mimimum_length_ngram= 2;
	
	//gold set parameters
	
	
	static String evaluation_path = "../data/gold_set/aol_query_reformulations/tm_click1.txt";
//	static String evaluation_path = "../data/gold_set/aol_query_reformulations/ydir/with_click.txt";
	//static String evaluation_path = "../data/gold_set/aol_query_reformulations/sample.txt";
	//static String evaluation_path="../data/gold_set/ydir_query_reformulations/with_long_click.txt";
	//static String evaluation_path="../data/gold_set/aol_query_reformulations/all_click_youth.txt";
	//static String evaluation_path="../data/gold_set/y_reformulations/years_10_12_sample.txt";
	//static String evaluation_path="../data/gold_set/y_reformulations/years_16_sample.txt";
	
	static String log_type="aol";
	static HashSet<String> valid_ages= new HashSet<String>();
	
	
	static boolean onlyClicks=true;
	static int duration =30;
	static boolean onlyManual=false;
	static String output_path="/home/sergio/projects/learning_rank/query_suggestions/dmoz/features/rw_ranks.txt";
	
	
	public static void main(String argsp[]) {

		
		output_path="/home/sergio/projects/learning_rank/query_suggestions/dmoz/features/rw_ranks_k.txt";
	
	//	valid_ages.add("kids");
		//valid_ages.add("kids-teens");
		//valid_ages.add("kids-teens-mteens");
	
		
		CommanLineParametersRW jct = new CommanLineParametersRW();
	    JCommander cmd = new JCommander(jct);
	    
	    
	  
	    
	   if(argsp.length>2){
	   try {
	        cmd.parse(argsp);
	   //     System.out.println("initializing parameters");
	        initParameters(jct);
	        //
	   } catch (ParameterException ex) {
	       System.out.println(ex.getMessage());
	        cmd.usage();
	    }
	    }else{
	    	//valid_ages.add("kids");
	    	//valid_ages.add("kids-teens");
		//	valid_ages.add("kids-teens-mteens");
	    	valid_ages.add("teens-mteens");
			//valid_ages.add("kids-entertainment");
			//valid_ages.add("dmoz_plus");
			//valid_ages.add("adults");
	    	
	    }
	
		EvaluateSuggestions eval = 
				new EvaluateSuggestions(evaluation_path,log_type, valid_ages,onlyClicks,duration, onlyManual, output_path);
	
		
		//HashSet<String> valid_ages= new HashSet<String>();
		
		//valid_ages.add("kids-teens-mteens");
		//valid_ages.add("kids-entertainment");
		//valid_ages.add("dmoz_plus");
		//valid_ages.add("adults");
		try {
			runModelExperiments(valid_ages, output_path,eval);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}
	
	public static void initParameters(CommanLineParametersRW jct){
		
	
		
		if(jct.getIterations()!=0)
			max_iterations=jct.getIterations();
		
		
		if(jct.getAlpha()!=null){
			
			alpha=Double.valueOf(jct.getAlpha());
		}
		
		if(jct.getEvaluation()!=null)
			evaluation_path=jct.getEvaluation();
		
		if(jct.getOutupt_path()!=null)
			output_path=jct.getOutupt_path();
		
		if(jct.getQuerytags()!=null)
			query_tags=jct.getQuerytags();
		
		if(jct.getLogtype()!=null)
			log_type=jct.getLogtype();
		
		
		if(jct.getNode()!=null)
			node_node=jct.getLogtype();
		
	
		if(jct.getGraph()!=null)
			path=jct.getGraph();
		
		
		if(jct.getDuration()!=0)
			duration=jct.getDuration();
		
		
		
		if(jct.getLevel()!=0)
			level=jct.getLevel();
		
		if(jct.getAges()!=null){
			String ages[] = jct.getAges().split(",");
			
			for(int i =0; i <ages.length; i++){
			//	System.out.println("Reading valid ages:"+ ages[i].trim());
				valid_ages.add(ages[i].trim());
			}
		
		}else{
			
		//	valid_ages.add("kids");
			valid_ages.add("kids-teens");
		//	valid_ages.add("kids-teens-mteens");
		}
		
		
		
		
		
		
	}
	
	
	
	
	public static void runModelExperiments(HashSet<String> valid_ages, String output_path, EvaluateSuggestions eval) throws IOException{
		
		//valid_ages.add("kids");
		//valid_ages.add("kids-teens-mteens");
		//valid_ages.add("adults");
	
		AgeAggregator agg= new AgeAggregator(valid_ages);
		
		topics_words=null;
		topics_docs=null;
		boolean dotopics=false;
		PageRankSuggestions page = new PageRankSuggestions(path,node_node,topics_words, topics_docs,topics_probabilities, agg,alpha,max_iterations,level); //ranker
		
		System.out.println("RUNNING MODEL FOR " + "\t"+ valid_ages);
		//RandomWalkLatentEngine page = new RandomWalkLatentEngine(path,node_node,topics_words, topics_docs,topics_probabilities, agg,alpha,max_iterations); //ranker

	
		HashSet<String> queries_to_eval=new HashSet<String>();
		
		initQueriesToEval(eval.getGoldStandard(), queries_to_eval);
		
		QueryTagsModel querytags = new QueryTagsModel(query_tags, queries_to_eval,query_tag_limit);
	
	
		QueryTopicsModel querytopics=null;
		
		if(dotopics){
		querytopics =new QueryTopicsModel(query_topics_path,queries_to_eval);
		}
		//load bank of queries and their tags
		//System.out.println("query models loaded");
		
		eval.performEvaluation(page, querytags, querytopics);
		
		
		
	}
	
	public static void initQueriesToEval( Hashtable<String,Hashtable<String, ArrayList<String>>> gold, HashSet<String>queries_to_eval){
		Enumeration<String> keys = gold.keys();
		while(keys.hasMoreElements()){
			
			String key =keys.nextElement();
			Hashtable<String, ArrayList<String>> qq = gold.get(key);
		
			queries_to_eval.addAll(qq.keySet());
			
		}
	}
}